<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="调用编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入调用编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="状态" size="small" :clearable="true">
            <el-option
              v-for="dict in dict.type.sys_bool_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
         <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['aipay:apiType:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="调用编号" align="center" prop="number" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_bool_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="商户默认开启" align="center" prop="defaultSwitch">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_bool_status" :value="scope.row.defaultSwitch"/>
        </template>
      </el-table-column>
      <el-table-column label="接口域名" align="center" prop="domain" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="默认规则" align="center" prop="ruleName" />
      <el-table-column label="添加时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aipay:apiType:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['aipay:apiType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="调用编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入调用编号" />
        </el-form-item>
        <el-form-item label="开启状态" prop="status">
          <el-select v-model="form.status" placeholder="开启状态" size="small">
            <el-option
              v-for="dict in dict.type.sys_bool_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="商户默认开启" prop="defaultSwitch">
          <el-select v-model="form.defaultSwitch" placeholder="商户默认开启" size="small">
            <el-option
              v-for="dict in dict.type.sys_bool_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="默认规则列表" prop="ruleId" v-show="form.id">
          <el-select v-model="form.ruleId" placeholder="默认规则列表" size="small">
            <el-option
              v-for="item in ruleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="接口域名" prop="domain">
          <el-input v-model="form.domain" placeholder="请输入接口域名" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPaymentType, getPaymentType, addPaymentType, updatePaymentType, delPaymentType  } from "@/api/aipay/paymentType";
import { listPaymentRuleByType  } from "@/api/aipay/paymentRule";

export default {
  name: "PaymentType",
  dicts: ['sys_bool_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //表格数据
      postList: [],
      roleList: [],
      ruleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      activeName: "public",
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        status: undefined,
        number: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        name: undefined,
        number: undefined,
        defaultSwitch: undefined,
        ruleId: undefined,
        domain: undefined,
        sort: undefined,
        status: undefined
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        number: [
          { required: true, message: "调用编号不能为空", trigger: "blur" }
        ],
        defaultSwitch: [
          { required: true, message: "商户默认开启状态不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listPaymentType(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListPaymentRuleByType(typeId){
      listPaymentRuleByType({typeId: typeId}).then(response => {
        this.ruleList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        number: undefined,
        defaultSwitch: undefined,
        ruleId: undefined,
        domain: undefined,
        sort: undefined,
        status: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getPaymentType(row.id).then(response => {
        this.form = response.data;
        this.form.status = this.form.status + '';
        this.form.defaultSwitch = this.form.defaultSwitch + '';
        this.open = true;
        this.title = "修改支付类型";
      });
      this.getListPaymentRuleByType(row.id);
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePaymentType(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPaymentType(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPaymentType(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
    
  }
};
</script>
